#include "mytreemodel.h"
#include <QSqlQuery>
#include <QSqlQueryModel>
//#include <QAbstractItemModel>
#include <QPixmap>
#include <QPainter>
#include <QSize>
#include <QDebug>

MyTreeModel::MyTreeModel(QObject *parent) :
    QSqlQueryModel(parent)
{
    setHeaderData(0, Qt::Horizontal, "Унікальний.\nномер");
    setHeaderData(1, Qt::Horizontal, "Назва");
    setHeaderData(2, Qt::Horizontal, "Ідентифік.\nстилю");
    setHeaderData(3, Qt::Horizontal, "Клментар");
}

Qt::ItemFlags MyTreeModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    if(index.column()>=0 && index.column() <4)
    {
        flags |= Qt::ItemIsDragEnabled;
    }
    return flags;
}

QVariant MyTreeModel::data(const QModelIndex &item, int role) const
{
    QVariant value = QSqlQueryModel::data(item, role);
    switch (role) {
        case Qt::DisplayRole:
        case Qt::EditRole: return value;
    }
    return value;
}

QModelIndex MyTreeModel::parent(const QModelIndex &child) const
{
    return QModelIndex();
}

void MyTreeModel::refresh()
{
    LOG( "MyTabModel::refresh", this->query().lastQuery(), "" );
    clear();
    this->query().exec();

    generateParents();

}

void MyTreeModel::generateParents()
{
    for (int i = 0; i < this->rowCount(); i++)
    { // перебираю всі елементи, формую список батьків

    }

}

void StyleIdDelegate::paint(QPainter *painter,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index) const
{
    if(index.data().canConvert<int>())
    {
        QPixmap icon;
        bool converted = false;
        if (index.data().toInt(&converted) == 1)
        { icon.load(":/icons/play.png"); }
        else { icon.load(":/icons/document_file.png"); }

        icon = icon.scaledToHeight(option.rect.height(), Qt::SmoothTransformation);
        QPoint tl(option.rect.topLeft());

        tl.setX(tl.x() + option.rect.width()/2 - sizeHint(option, index).width()/2);
        painter->drawPixmap(tl, icon);
    }
    else
    QStyledItemDelegate::paint(painter, option, index);
}

QSize StyleIdDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(24, 24);
}

StyleIdDelegate::StyleIdDelegate(QObject *parent)
{
}

void MyTreeModel::fun()
{

    qDebug()<<"rowcount: "<<this->rowCount();
    //while (i<this->rowCount())
    //    this->fetchMore();
    //data()
}

void MyTreeModel::setupModelData(TreeItem *parentItem)
{
    QList<TreeItem*> parents;
    QList<int> inds;
    parents << parentItem;
    inds << 0;

    int num = 0;

    while (num < this->rowCount())
    {
        //int pos = 0;
        TreeItem *par = parents.last();
        par->insertChildren(par->childCount(), 1, this->columnCount());


        for (int col = 0; col < this->columnCount(); ++col)
            par->child(par->childCount()-1)->setData(col, data(index(num, col), Qt::DisplayRole));
    }

}
